package demo1.service;

import demo1.dao.Mapper.goodsMapper;
import demo1.dao.Mapper.userMapper;
import demo1.dao.sql.goodsSql;
import demo1.dao.sql.userSql;
import demo1.pojo.*;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

public class userService {
    public int checkLogin(users users) throws Exception {
        String userName = users.getUserName();
        String userPwd = users.getUserPwd();
        userMapper userMapper = new userSql();
        //ResultSet rs = userMapper.checkNickName(userName, userPwd);//手机号登录
        ResultSet rs = userMapper.checkPhone(userName, userPwd);//手机号登录
        int num = -1;
        if(rs.next()){
            //用户存在
            num = 1;
        }else {
            num = 0;
        }
        int num2 = -1;
        ResultSet rs2 = userMapper.checkUserAccount(userName, userPwd);//账户登录
        if(rs2.next()){
            num2 = 1;
        }
        else {
            num2 = 0;
        }

        if(num == 1){
            //昵称登录
            return 1;
        }
        else if(num2 == 1){
            //账户登录
            return 2;
        }
        else {
            return 0;
        }
    }
    public int register(users users)throws Exception{//用户注册
        String userNickName = users.getUserNickName();
        //两次密码能不能对应得上，密码有没有空格，昵称有没有重复有没有空格，新增一个账号，检查账户有没有重复，注册商户为1，注册用户为0，手机号是不是11位数字,查询手机号有没有重复

        String registerType = users.getRegisterType();
        String userPhone = users.getUserPhone();
        String pass = users.getPass();
        String checkPass = users.getCheckpass();

        int userNickNameLenth = userNickName.length();//初始字符串长度
        int passLenth = pass.length();//初始字符串长度
        int checkPassLenth = checkPass.length();//初始字符串长度

        userNickName = userNickName.replaceAll("\\s+", "");//去除所有的空格
        userPhone = userPhone.replaceAll("\\s+", "");//去除所有的空格
        pass = pass.replaceAll("\\s+", "");//去除所有的空格
        checkPass = checkPass.replaceAll("\\s+", "");//去除所有的空格


        int userNickNameLenth2 = userNickName.length();//后来的字符串长度
        int passLenth2 = pass.length();//后来的字符串长度
        int checkPassLenth2 = checkPass.length();//后来的字符串长度

        if (!pass.equals(checkPass) || (passLenth != passLenth2) || (checkPassLenth2 != checkPassLenth) || (userNickNameLenth != userNickNameLenth2)) {
            //两次密码不一样,密码中含有空格,昵称含有空格,返回0
            return 0;
        }
        else {
            //手机号不是11位数字
            if(!userPhone.matches("\\d{11}")){
                return 0;
            }
            else {
                userMapper userMapper = new userSql();
                ResultSet rs = userMapper.selectAll();
                while (rs.next()) {
                    //检查昵称有没有重复，手机号有没有重复
                    String nickName = rs.getString("user_nickname");
                    String phone = rs.getString("user_phone");
                    if (Objects.equals(nickName, userNickName)) {
                        //昵称重复
                        return 2;
                    }
                    if (Objects.equals(phone, userPhone)) {
                        //手机号重复
                        return 3;
                    }
                    if (passLenth2 < 6) {
                        //密码不能少于6位
                        return 4;
                    }
                    if (userNickNameLenth2 < 3) {
                        //昵称不能少于3个字
                        return 5;
                    }
                }
                //开始写入数据库
                int num = 0;//默认注册为用户
                if (Objects.equals(registerType, "注册为商户")) {
                    num = 1;
                }
                //检查账号有没有重复
                String accountNumber;
                while (true) {
                    //为用户新增一个账号
                    Random rand = new Random(); // 初始化随机数生成器
                    accountNumber = "";
                    for (int i = 0; i < 8; i++) { // 循环8次
                        accountNumber += rand.nextInt(10); // 每次循环生成一个0-9的随机数并添加到字符串中
                    }
                    int temp = 1;
                    ResultSet rs2 = userMapper.selectAll();
                    while (rs2.next()) {
                        if (accountNumber.equals(rs2.getString("user_account"))) {
                            //如果重复了
                            temp = 0;
                        }
                    }
                    if (temp == 1) {//没有重复的账号
                        break;
                    }
                }
                userMapper.insertUser(userNickName, num, userPhone, pass,accountNumber);//写入
                return 1;
            }
        }
    }
    public int modifyUser(users users)throws Exception {//修改用户数据
            String userNickName = users.getUserNickName();
            String userName = users.getUserName();
            String userPhone = users.getUserPhone();
            String userDo = users.getUserDo();
            String shopName = users.getShopName();
            int userID = users.getUserID();

            int userNickNameLenth = userNickName.length();//初始字符串长度
            int userNameLenth = userName.length();//初始字符串长度
            int shopNameLenth = shopName.length();//初始字符串长度

            userNickName = userNickName.replaceAll("\\s+", "");//去除所有的空格
            userPhone = userPhone.replaceAll("\\s+", "");//去除所有的空格
            userName = userName.replaceAll("\\s+", "");//去除所有的空格
            shopName = shopName.replaceAll("\\s+", "");//去除所有的空格


            int userNickNameLenth2 = userNickName.length();//后来的字符串长度
            int userNameLenth2 = userName.length();//后来的字符串长度
            int shopNameLenth2 = shopName.length();//后来的字符串长度

            if ((userNickNameLenth != userNickNameLenth2) ||(userNameLenth != userNameLenth2) || (shopNameLenth != shopNameLenth2)) {
                //两次密码不一样,密码中含有空格,昵称含有空格,姓名含有空格,返回0
                return 0;
            } else {
                //手机号不是11位数字
                if (!userPhone.matches("\\d{11}")) {
                    return 0;
                } else {
                    userMapper userMapper = new userSql();
                    ResultSet rs = userMapper.selectAll();
                    while (rs.next()) {
                        int userid = rs.getInt("user_id");
                        if(userid == userID){
                            continue;
                        }
                        else {
                            //检查昵称有没有重复，手机号有没有重复
                            String nickName = rs.getString("user_nickname");
                            String phone = rs.getString("user_phone");
                            String shopName2 = rs.getString("shop_name");
                            if (Objects.equals(nickName, userNickName)) {
                                //昵称重复
                                return 2;
                            }
                            if (Objects.equals(phone, userPhone)) {
                                //手机号重复
                                return 3;
                            }
                            if (userNameLenth2 < 2) {
                                //真实姓名不能少于2位
                                return 4;
                            }
                            if (userNickNameLenth2 < 3) {
                                //昵称不能少于3个字
                                return 5;
                            }
                            if (Objects.equals(shopName2, shopName)) {
                                //小店名称重复
                                return 6;
                            }
                            if (shopNameLenth2 < 3) {
                                //昵称不能少于3个字
                                return 7;
                            }
                        }
                    }
                    //开始写入数据库,把商品的小店名称也改了
                    goodsMapper goodsMapper = new goodsSql();
                    goodsMapper.modifyGoodsShopName(shopName,userID);

                    userMapper.updateUser(userNickName, userPhone, userName, userDo,userID,shopName);//写入
                    return 1;
                }
            }
    }
    public int modifyUser2(users users)throws Exception {//修改用户数据
        String userNickName = users.getUserNickName();
        String userName = users.getUserName();
        String userPhone = users.getUserPhone();
        String userDo = users.getUserDo();
        String shopName = users.getShopName();
        int userID = users.getUserID();

        int userNickNameLenth = userNickName.length();//初始字符串长度
        int shopNameLenth = shopName.length();//初始字符串长度

        userNickName = userNickName.replaceAll("\\s+", "");//去除所有的空格
        userPhone = userPhone.replaceAll("\\s+", "");//去除所有的空格
        shopName = shopName.replaceAll("\\s+", "");//去除所有的空格


        int userNickNameLenth2 = userNickName.length();//后来的字符串长度
        int shopNameLenth2 = shopName.length();//后来的字符串长度

        if ((userNickNameLenth != userNickNameLenth2) || (shopNameLenth != shopNameLenth2)) {
            //两次密码不一样,密码中含有空格,昵称含有空格,姓名含有空格,返回0
            return 0;
        } else {
            //手机号不是11位数字
            if (!userPhone.matches("\\d{11}")) {
                return 0;
            } else {
                userMapper userMapper = new userSql();
                ResultSet rs = userMapper.selectAll();
                while (rs.next()) {
                    int userid = rs.getInt("user_id");
                    if(userid == userID){
                        continue;
                    }
                    else {
                        //检查昵称有没有重复，手机号有没有重复
                        String nickName = rs.getString("user_nickname");
                        String phone = rs.getString("user_phone");
                        String shopName2 = rs.getString("shop_name");
                        if (Objects.equals(nickName, userNickName)) {
                            //昵称重复
                            return 2;
                        }
                        if (Objects.equals(phone, userPhone)) {
                            //手机号重复
                            return 3;
                        }
                        if (userNickNameLenth2 < 3) {
                            //昵称不能少于3个字
                            return 5;
                        }
                        if (Objects.equals(shopName2, shopName)) {
                            //小店名称重复
                            return 6;
                        }
                        if (shopNameLenth2 < 3) {
                            //昵称不能少于3个字
                            return 7;
                        }
                    }
                }
                //开始写入数据库,把商品的小店名称也改了
                goodsMapper goodsMapper = new goodsSql();
                goodsMapper.modifyGoodsShopName(shopName,userID);

                userMapper.updateUser(userNickName, userPhone, userName, userDo,userID,shopName);//写入
                return 1;
            }
        }
    }

    public int modifyUserPass(users users) throws Exception {//修改用户数据
        String pass = users.getPass();
        String checkPass = users.getCheckpass();
        int userID = users.getUserID();

        int passLenth = pass.length();//初始字符串长度
        int checkPassLenth = checkPass.length();//初始字符串长度

        pass = pass.replaceAll("\\s+", "");//去除所有的空格
        checkPass = checkPass.replaceAll("\\s+", "");//去除所有的空格


        int passLenth2 = pass.length();//后来的字符串长度
        int checkPassLenth2 = checkPass.length();//后来的字符串长度

        if (!pass.equals(checkPass) || (passLenth != passLenth2) || (checkPassLenth2 != checkPassLenth)) {
            //两次密码不一样,密码中含有空格返回0
            return 0;
        } else {
            userMapper userMapper = new userSql();
                if (passLenth2 < 6) {
                    //密码不能少于6位
                    return 2;
                }
            //开始写入数据库
            userMapper.updateUserPass(pass, userID);//写入
            return 1;
        }
    }
    public users loginUserBuyNickName(users users)throws Exception{//通过昵称获取全部数据
        userMapper userMapper = new userSql();
        return userMapper.selectUserByNickName(users.getUserName());
    }
    public users loginUserByPhone(users users)throws Exception{//通过手机号获取全部数据
        userMapper userMapper = new userSql();
        return userMapper.selectUserByPhone(users.getUserName());
    }
    public users loginUserBuyAccount(users users)throws Exception{//通过账号获取全部数据
        userMapper userMapper = new userSql();
        return userMapper.selectUserByAccount(users.getUserName());
    }
    public users sendAccount(users users)throws Exception{//通过昵称获取全部数据
        userMapper userMapper = new userSql();
        return userMapper.selectUserByNickName(users.getUserNickName());
    }
    public List<AddressTableData> showAllAddress(users users)throws Exception{//获取所有的地址
        userMapper userMapper = new userSql();
        return userMapper.selectUserAddress(users.getUserID());
    }
    public address showAllMorenAddress(users users)throws Exception{//获取所有的地址
        userMapper userMapper = new userSql();
        return userMapper.selectUserMorenAddress(users.getUserID());
    }
    public int addAddress(address address, int userID)throws Exception {//新增地址
        String userAddress = address.getUserAddress();

        int userAddressLenth = userAddress.length();//初始字符串长度

        userAddress = userAddress.replaceAll("\\s+", "");//去除所有的空格

        int userAddressLenth2 = userAddress.length();//后来的字符串长度

        if ((userAddressLenth != userAddressLenth2) || (userAddress.isEmpty())) {
            //地址含有空格,或者地址为空,返回0
            return 0;
        }
        else {
            //符合条件，检查一下有没有地址，如果是第一个地址，把地址也设置为默认地址
            userMapper userMapper = new userSql();
            ResultSet rs = userMapper.selectAllAddressById(userID);
            if(rs.next()){
                //如果存在地址，不需要写入默认地址
                userMapper.insertUserAddress(userAddress,userID);//写入
            }
            else {
                //如果不存在
                userMapper.updateUserAddress(userAddress,userID);//更新默认地址
                userMapper.insertUserAddress(userAddress,userID);//写入
            }
            return 1;
        }
    }
    public int deleteAddress(int addressID) throws Exception {//删除地址
        //删除地址前还要先检查一下有没有删除默认地址，如果删除了默认地址还要随机选一个地址当默认地址
        userMapper userMapper = new userSql();
        ResultSet rs = userMapper.selectAddressById(addressID);
        int userID = -1;
        String userAddress = null;
        if (rs.next()) {
            userAddress = rs.getString("user_address");
            userID = rs.getInt("user_id");
        }
        ResultSet rs2 = userMapper.selectAllById(userID);
        String userAddress2 = null;
        if (rs2.next()) {
            userAddress2 = rs2.getString("user_address");
        }
        if (Objects.equals(userAddress, userAddress2)){
            //删除的地址就是默认地址，把默认地址换一个
            userMapper.deleteAddress(addressID);//先删除了，再查找全部
            ResultSet rs3 = userMapper.selectAllAddress();
            if(rs3.next()){//如果还有地址
                String userAddress3 = rs3.getString("user_address");
                userMapper.updateUserAddress(userAddress3,userID);//把第一个地址存入默认地址
            }
            else {
                //已经没有地址了
                userMapper.updateUserAddress("",userID);//把空字符串存入默认地址
            }
        }else {
            //如果删除的不是默认地址
            userMapper.deleteAddress(addressID);
        }
        return 1;
    }
    public int modifyAddress(String userAddress,int addressID,int userID)throws Exception{//更新地址
        int userAddressLenth = userAddress.length();//初始字符串长度

        userAddress = userAddress.replaceAll("\\s+", "");//去除所有的空格

        int userAddressLenth2 = userAddress.length();//后来的字符串长度

        if ((userAddressLenth != userAddressLenth2) || (userAddress.isEmpty())) {
            //地址含有空格,或者地址为空,返回0
            return 0;
        }
        else {
            userMapper userMapper = new userSql();
            //更新地址前先确认一下是不是修改的默认地址
            ResultSet rs2 = userMapper.selectAllById(userID);
            ResultSet rs3 = userMapper.selectAddressById(addressID);
            String userAddress2 = null;//默认地址
            String userAddress3 = null;//原来的地址
            if (rs2.next()) {
                userAddress2 = rs2.getString("user_address");
            }
            if(rs3.next()){
                userAddress3 = rs3.getString("user_address");
            }
            if(Objects.equals(userAddress2, userAddress3)){
                //如果修改的是默认地址，那么默认地址一起改变
                userMapper.modifyUserAddress(userAddress,addressID);//修改地址
                userMapper.updateUserAddress(userAddress,userID);//把修改后的地址存入默认地址
            }
            else {
                //修改的不是默认地址
                userMapper.modifyUserAddress(userAddress,addressID);//修改地址
            }
            return 1;
        }
    }

    public int modifyMorenAddress(String userAddress,int userID) throws Exception {//更新地址
        userMapper userMapper = new userSql();
        userMapper.updateUserAddress(userAddress, userID);//把修改后的地址存入默认地址
        return 1;
    }
    public int addToGouwucar(gouwucar gouwucar)throws Exception {//添加到购物车
        String userAddress = gouwucar.getUserAddress();
        String goodsName = gouwucar.getGoodsName();
        String goodsMessage = gouwucar.getGoodsMessage();
        int userID = gouwucar.getUserID();
        int price = gouwucar.getPrice();
        int goodsID = gouwucar.getGoodsID();

        int goodsNum = gouwucar.getGoodsNum();//购买的商品数


        if ((userAddress.isEmpty()) || goodsNum > 5 || goodsNum <= 0) {
            //地址为空,商品数大于5，商品数小于0，商品数为空，返回0
            return 0;
        }
        else {
            //符合条件，检查一下购物车里面有没有该商品，如果有，读取之前的商品数，然后加上现在的商品数存进数据库
            userMapper userMapper = new userSql();
            ResultSet rs = userMapper.selectGouwucarById(goodsID,userID);
            if(rs.next()){
                //如果存在商品，读取商品数然后加上现在的存入数据库
                int num = rs.getInt("goods_num");
                goodsNum += num;
                userMapper.updateGouwucarGoodsNum(goodsNum,userID,goodsID);//更新购物车的商品数
            }
            else {
                //如果不存在
                userMapper.insertGouwucarGoods(userID,price,goodsMessage,goodsName,goodsNum,goodsID,userAddress);//写入
            }
            return 1;
        }
    }
    public List<gouwucar> showMyGouwucar(int userID) throws Exception {
        userMapper userMapper = new userSql();
        return userMapper.selectGouwucar(userID);
    }
    public int deleteGouwucarGoods(int goodsID,int userID) throws Exception {
        userMapper userMapper = new userSql();
        userMapper.deleteGouwucarGoods(goodsID,userID);
        return 1;
    }
    public int deleteAllGouwucarGoods(int userID) throws Exception {
        userMapper userMapper = new userSql();
        userMapper.deleteAllGouwucarGoods(userID);
        return 1;
    }
    public int addOrderForm(gouwucar gouwucar,users users)throws Exception{//添加订单
        goodsMapper goodsMapper = new goodsSql();
        ResultSet rs3 = goodsMapper.selectByID(gouwucar.getGoodsID());
        int num = -1;
        int goodsXiaoLiang = -1;
        if (rs3.next()) {
            num = rs3.getInt("goods_num");
            goodsXiaoLiang = rs3.getInt("goods_xiaoliang");
        }
        if ((gouwucar.userAddress.isEmpty()) || gouwucar.goodsNum > 5 || gouwucar.goodsNum <= 0) {
            //地址为空,商品数大于5，商品数小于0，商品数为空，返回0
            return 0;
        }
        else if (num == 0) {
            //库存为0,不能购买
            return 3;
        }
        else if (num < gouwucar.getGoodsNum()) {
            //库存不足，显示库存剩余件数
            return 4;
        }
        else {
            //先检查钱够不够
            userMapper userMapper = new userSql();
            ResultSet rs = userMapper.selectAllById(users.getUserID());
            int money = 0;
            if (rs.next()) {
                money = rs.getInt("money");
            }
            ResultSet rs2 = userMapper.selectGouwucarById(gouwucar.goodsID, users.getUserID());
            int totalPrice = 0;
            int price = 0;
            int goodsNum = 0;
            if (rs2.next()) {
                price = rs2.getInt("price");//商品单价
            }
            goodsNum = gouwucar.getGoodsNum();//后来的商品数优先
            totalPrice = price * goodsNum;//总价格
            if (money < totalPrice){
                if(num > gouwucar.getGoodsNum()){
                    //库存足够
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    //钱不够,订单状态设置为未支付，statue = 0
                    userMapper.insertOrderFormGoods(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //一旦生成订单，就把商品从购物车中移除,库存减
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                }
                else if(num == gouwucar.getGoodsNum()){
                    //库存刚刚好，要调整为下架，
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsStatue("下架",gouwucar.getGoodsID());
                    //钱不够,订单状态设置为未支付，statue = 0
                    userMapper.insertOrderFormGoods(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //一旦生成订单，就把商品从购物车中移除,库存减
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                }
                return 2;
            }
            else {
                if(num > gouwucar.getGoodsNum()){
                    //库存足够,减少库存，增加销量
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsXiaoLiang(goodsXiaoLiang+gouwucar.getGoodsNum(),gouwucar.getGoodsID());//增加销量
                    //钱够了,statue = 1
                    userMapper.insertOrderFormGoodsSuccess(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //现在还要把钱减少,一旦生成订单，就把商品从购物车中移除
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                    userMapper.updateUserMoney(money - totalPrice, users.getUserID());
                    users.setMoney(money - totalPrice);
                    //把钱加到商家手里
                    ResultSet rs30 = goodsMapper.selectByID(gouwucar.getGoodsID());//获取商家id
                    int userID = 0;
                    if(rs30.next()){
                        userID = rs30.getInt("id");
                    }
                    ResultSet rs20 = userMapper.selectAllById(userID);//把商家现在的钱查询一下
                    int money2 = 0;
                    if(rs20.next()){
                        money2 = rs20.getInt("money");
                    }
                    userMapper.updateUserMoney(money2 + gouwucar.getPrice(),userID);//商家ID
                }
                else if(num == gouwucar.getGoodsNum()){
                    //库存刚刚好，要调整为下架，增加销量
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsStatue("下架",gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsXiaoLiang(goodsXiaoLiang+gouwucar.getGoodsNum(),gouwucar.getGoodsID());//增加销量
                    //钱够了,statue = 1
                    userMapper.insertOrderFormGoodsSuccess(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //现在还要把钱减少,一旦生成订单，就把商品从购物车中移除
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                    userMapper.updateUserMoney(money - totalPrice, users.getUserID());
                    users.setMoney(money-totalPrice);
                    //把钱加到商家手里
                    ResultSet rs30 = goodsMapper.selectByID(gouwucar.getGoodsID());//获取商家id
                    int userID = 0;
                    if(rs30.next()){
                        userID = rs30.getInt("id");
                    }
                    ResultSet rs20 = userMapper.selectAllById(userID);//把商家现在的钱查询一下
                    int money2 = 0;
                    if(rs20.next()){
                        money2 = rs20.getInt("money");
                    }
                    userMapper.updateUserMoney(money2 + gouwucar.getPrice(),userID);//商家ID
                }
                return 1;
            }
        }
    }
    public int addAllToOrderForm(int[] ids,users users)throws Exception{//添加订单
        int totalPrice = 0;
        int sum = 0;
        int price = 0;
        int goodsNum = 0;
        int money = 0;
        //添加到订单前要先检查一下库存是不是为0
        for (int i = 0; i < ids.length; i++) {
            userMapper userMapper = new userSql();
            gouwucar gouwucar = userMapper.selectGouwucarGoods(ids[i], users.getUserID());
            goodsMapper goodsMapper = new goodsSql();
            ResultSet rs3 = goodsMapper.selectByID(gouwucar.getGoodsID());
            int num = -1;
            if (rs3.next()) {
                num = rs3.getInt("goods_num");
            }
            if ((gouwucar.userAddress.isEmpty()) || gouwucar.goodsNum <= 0) {
                //地址为空,商品数小于0，商品数为空，返回0
                return 0;
            }
            else if (num == 0) {
                //库存为0,从购物车中删除该商品
                userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
            } else {
                //先检查钱够不够,检查商品数量够不够
                ResultSet rs = userMapper.selectAllById(users.getUserID());

                if (rs.next()) {
                    money = rs.getInt("money");
                }
                ResultSet rs2 = userMapper.selectGouwucarById(gouwucar.goodsID, users.getUserID());
                if (rs2.next()) {
                    price = rs2.getInt("price");//商品单价
                }

                goodsNum = gouwucar.getGoodsNum();//后来的商品数优先
                totalPrice = price * goodsNum;//总价格
                sum += totalPrice;
            }
        }
        if (money < sum) {//钱不够
            for (int i = 0; i < ids.length; i++) {
                userMapper userMapper = new userSql();
                gouwucar gouwucar = userMapper.selectGouwucarGoods(ids[i], users.getUserID());
                goodsMapper goodsMapper = new goodsSql();
                int num = -1;
                ResultSet rs3 = goodsMapper.selectByID(gouwucar.getGoodsID());
                if (rs3.next()) {
                    num = rs3.getInt("goods_num");
                }
                if(num > gouwucar.getGoodsNum()){
                    //库存足够
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    totalPrice = gouwucar.getPrice()*gouwucar.getGoodsNum();
                    //钱不够,订单状态设置为未支付，statue = 0
                    userMapper.insertOrderFormGoods(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //一旦生成订单，就把商品从购物车中移除,库存减
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                }
                else if(num == gouwucar.getGoodsNum()){
                    //库存刚刚好，要调整为下架，
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsStatue("下架",gouwucar.getGoodsID());
                    totalPrice = gouwucar.getPrice()*gouwucar.getGoodsNum();
                    //钱不够,订单状态设置为未支付，statue = 0
                    userMapper.insertOrderFormGoods(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //一旦生成订单，就把商品从购物车中移除,库存减
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());

                }
            }
            return 2;
        }
        else {//钱够了
            for (int i = 0; i < ids.length; i++) {
                userMapper userMapper = new userSql();
                gouwucar gouwucar = userMapper.selectGouwucarGoods(ids[i], users.getUserID());
                goodsMapper goodsMapper = new goodsSql();
                int num = -1;
                int goodsXiaoLiang = -1;
                ResultSet rs3 = goodsMapper.selectByID(gouwucar.getGoodsID());
                if (rs3.next()) {
                    num = rs3.getInt("goods_num");
                    goodsXiaoLiang = rs3.getInt("goods_xiaoliang");
                }
                if(num > gouwucar.getGoodsNum()){
                    //库存足够,减少库存，增加销量
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsXiaoLiang(goodsXiaoLiang+gouwucar.getGoodsNum(),gouwucar.getGoodsID());//增加销量
                    totalPrice = gouwucar.getPrice()*gouwucar.getGoodsNum();
                    //钱够了,statue = 1
                    userMapper.insertOrderFormGoodsSuccess(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //现在还要把钱减少,一旦生成订单，就把商品从购物车中移除
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                    userMapper.updateUserMoney(money - sum, users.getUserID());
                    users.setMoney(money - sum);
                    //把钱加到商家手里
                    ResultSet rs30 = goodsMapper.selectByID(gouwucar.getGoodsID());//获取商家id
                    int userID = 0;
                    if(rs30.next()){
                        userID = rs30.getInt("id");
                    }
                    ResultSet rs20 = userMapper.selectAllById(userID);//把商家现在的钱查询一下
                    int money2 = 0;
                    if(rs20.next()){
                        money2 = rs20.getInt("money");
                    }
                    userMapper.updateUserMoney(money2 + gouwucar.getPrice(),userID);//商家ID
                }
                else if(num == gouwucar.getGoodsNum()){
                    //库存刚刚好，要调整为下架，增加销量
                    goodsMapper.modifyGoodsNum(num-gouwucar.getGoodsNum(),gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsStatue("下架",gouwucar.getGoodsID());
                    goodsMapper.modifyGoodsXiaoLiang(goodsXiaoLiang+gouwucar.getGoodsNum(),gouwucar.getGoodsID());//增加销量
                    totalPrice = gouwucar.getPrice()*gouwucar.getGoodsNum();
                    //钱够了,statue = 1
                    userMapper.insertOrderFormGoodsSuccess(users.getUserID(), gouwucar.getPrice(), totalPrice, gouwucar.getGoodsID(), gouwucar.getGoodsName(), gouwucar.getGoodsMessage(), gouwucar.getGoodsNum(), gouwucar.getUserAddress(), users.getUserPhone());
                    //现在还要把钱减少,一旦生成订单，就把商品从购物车中移除
                    userMapper.deleteGouwucarGoods(gouwucar.getGoodsID(), users.getUserID());
                    userMapper.updateUserMoney(money - sum, users.getUserID());
                    users.setMoney(money - sum);
                    //把钱加到商家手里
                    ResultSet rs30 = goodsMapper.selectByID(gouwucar.getGoodsID());//获取商家id
                    int userID = 0;
                    if(rs30.next()){
                        userID = rs30.getInt("id");
                    }
                    ResultSet rs20 = userMapper.selectAllById(userID);//把商家现在的钱查询一下
                    int money2 = 0;
                    if(rs20.next()){
                        money2 = rs20.getInt("money");
                    }
                    userMapper.updateUserMoney(money2 + gouwucar.getPrice(),userID);//商家ID
                }
            }
            return 1;
        }
    }
    public int payOrderForm(int[] ids,users users)throws Exception{//批量付款
        int totalPrice = 0;
        int sum = 0;
        int money = 0;
        int temp = 1;
        for (int i = 0; i < ids.length; i++) {
            userMapper userMapper = new userSql();
            orderForm orderForm = userMapper.selectOrderFormGoods(ids[i]);
            if ((orderForm.userAddress.isEmpty())|| orderForm.goodsNum <= 0) {
                //地址为空,商品数大于5，商品数小于0，商品数为空，返回0
                return 0;
            } else {
                //先检查钱够不够
                ResultSet rs = userMapper.selectAllById(users.getUserID());

                if (rs.next()) {
                    money = rs.getInt("money");
                }
                sum += orderForm.getTotalPrice();
                //检查时间有没有过
                Timestamp time = orderForm.getTime();
                // 获取当前时间的Date
                Date currentDate = new Date();

                // 比较时间
                if (time.before(currentDate)) {
                    //时间已经过了
                    temp = 0;
                }
            }
        }
        if(temp == 1){
            //所有的订单时间都没有过
            if (money < sum) {
                    //钱不够,订单状态设置为未支付，statue = 0，
                return 2;
            }
            else {
                for (int i = 0; i < ids.length; i++) {
                    userMapper userMapper = new userSql();
                    orderForm orderForm = userMapper.selectOrderFormGoods(ids[i]);
                    //钱够了,statue = 1
                    userMapper.updateOrderFormStatue(ids[i]);
                    int sum1 = orderForm.getTotalPrice();
                    //现在还要把钱减少
                    userMapper.updateUserMoney(money - sum, users.getUserID());
                    users.setMoney(money - sum);
                    //把钱加到商家手里
                    goodsMapper goodsMapper = new goodsSql();
                    ResultSet rs3 = goodsMapper.selectByID(orderForm.getGoodsID());//获取商家id
                    int userID = 0;
                    if(rs3.next()){
                        userID = rs3.getInt("id");
                    }
                    ResultSet rs2 = userMapper.selectAllById(userID);//把商家现在的钱查询一下
                    int money2 = 0;
                    if(rs2.next()){
                        money2 = rs2.getInt("money");
                    }
                    userMapper.updateUserMoney(money2 + sum1,userID);//商家ID
                }
                return 1;
            }
        }
        else {//存在有订单已经过期
            return 3;
        }
    }

    public int payOneForm(orderForm orderForm, users users) throws Exception {//单个付款
        int sum = 0;
        int money = 0;
        int temp = 1;
        userMapper userMapper = new userSql();
        if ((orderForm.userAddress.isEmpty()) || orderForm.goodsNum <= 0) {
            //地址为空,商品数大于5，商品数小于0，商品数为空，返回0
            return 0;
        } else {
            //先检查钱够不够
            ResultSet rs = userMapper.selectAllById(users.getUserID());

            if (rs.next()) {
                money = rs.getInt("money");
            }
            sum += orderForm.getTotalPrice();
            //检查时间有没有过
            Timestamp time = orderForm.getTime();
            // 获取当前时间的Date
            Date currentDate = new Date();

            // 比较时间
            if (time.before(currentDate)) {
                //时间已经过了
                temp = 0;
            }
        }
        if (temp == 1) {
            //所有的订单时间都没有过
            if (money < sum) {
                //钱不够,订单状态设置为未支付，statue = 0，
                return 2;
            } else {
                //钱够了,statue = 1
                userMapper.updateOrderFormStatue(orderForm.getOrderID());
                //现在还要把钱减少
                userMapper.updateUserMoney(money - sum, users.getUserID());
                users.setMoney(money - sum);
                //把钱加到商家手里
                goodsMapper goodsMapper = new goodsSql();
                ResultSet rs3 = goodsMapper.selectByID(orderForm.getGoodsID());//获取商家id
                int userID = 0;
                if(rs3.next()){
                    userID = rs3.getInt("id");
                }
                ResultSet rs2 = userMapper.selectAllById(userID);//把商家现在的钱查询一下
                int money2 = 0;
                if(rs2.next()){
                    money2 = rs2.getInt("money");
                }
                userMapper.updateUserMoney(money2 + sum,userID);//商家ID
                return 1;
            }
        } else {//存在有订单已经过期
            return 3;
        }
    }
    public List<orderForm> showFalseOrderForm(users user) throws Exception {
        userMapper userMapper = new userSql();
        return userMapper.selectOrderForm(user.getUserID());
    }
    public List<orderForm> showAllOrderForm(users user) throws Exception {
        userMapper userMapper = new userSql();
        return userMapper.selectAllOrderForm(user.getUserID());
    }
    public List<orderForm> showSuccessOrderForm(users user) throws Exception {
        userMapper userMapper = new userSql();
        return userMapper.selectSuccessOrderForm(user.getUserID());
    }
    public int modifyOrderFormAddress(orderForm orderForm)throws Exception{
        userMapper userMapper = new userSql();
        userMapper.modifyOrderFormAddress(orderForm);
        return 1;
    }
    public int deleteOrderForm(int ids) throws Exception {//取消订单
        userMapper userMapper = new userSql();
        userMapper.deleteOrderForm(ids);
        return 1;
    }
    public int deleteOrderForm2(int ids) throws Exception {//删除订单
        userMapper userMapper = new userSql();
        //如果是未支付不可以删除
        ResultSet rs = userMapper.selectAllOrder(ids);
        if(rs.next()){
            if(rs.getInt("statue") == 0) {//状态为0，就是没有支付成功的
                Timestamp time = rs.getTimestamp("time");//商品上架时间
                // 获取当前时间的Date
                Date currentDate = new Date();
                // 比较时间
                if (time.before(currentDate)) {
                } else {
                    //orderForm.setStatueOrderForm("待支付");
                    return 2;
                }
            }
        }
        userMapper.deleteOrderForm2(ids);
        return 1;
    }
    public int addPrice(int ids,users users) throws Exception {//把充值提交给管理
        if(ids <= 0){
            return 0;
        }
        userMapper userMapper = new userSql();
        userMapper.insertPriceStatue(ids,users);
        return 1;
    }
    public List<cancelOrder> showCancelOrder(users user) throws Exception {
        goodsMapper goodsMapper = new goodsSql();
        return goodsMapper.selectCancelOrderById(user.getUserID());
    }
    public int signIn(users users)throws Exception{//签到
        userMapper userMapper = new userSql();
        //签到前需要先查询看看今天签到过没有
//        // 获取当前时间
        Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());

        // 将Timestamp转换为LocalDateTime
        LocalDateTime localDateTime = currentTimestamp.toLocalDateTime();

        // 将LocalDateTime的小时、分钟、秒和纳秒都设置为0
        LocalDateTime date = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
        // 将LocalDateTime转换回Timestamp
        Timestamp times = Timestamp.valueOf(date);
        LocalDateTime localDate = times.toLocalDateTime().plusDays(1);
        Timestamp times2 = Timestamp.valueOf(localDate);

        ResultSet rs = userMapper.selectSignIn(users.getUserID());
        int num = 0;
        while (rs.next()){
            Timestamp time = rs.getTimestamp("time");
            if (time.before(currentTimestamp)){
                //时间过了
            }
            else {
                num = 1;
            }
        }
        if(num == 0){
            //今天还没有签到
            ResultSet rs2 = userMapper.selectAllById(users.getUserID());//查询原来有多少钱
            if(rs2.next()){
                int money = rs2.getInt("money");
                userMapper.updateUserMoney(money+2,users.getUserID());//更新钱
                users.setMoney(money+2);
            }
            userMapper.insertSignIn(users.getUserNickName(),users.getUserID(),times2);//签到
            return 1;
        }
        return 0;
    }
    public int changeUserStatue(users users)throws Exception{
        userMapper userMapper = new userSql();
        //发送申请前先查询一下，是不是已经发过申请了
        ResultSet rs = userMapper.selectAllChangeUser(users.getUserID());
        if(rs.next()){
            //如果查询到了
            return 0;
        }
        userMapper.insertChangeUserStatue(users.getUserID());//发送申请
        return 1;
    }
    public List<users> showAllUsers()throws Exception{
        userMapper userMapper = new userSql();
        return userMapper.selectAllUsers();
    }
    public List<changeUser> showAllChangeUsers()throws Exception{
        userMapper userMapper = new userSql();
        return userMapper.selectAllChangeUsers();
    }
    public int agreeChangeUser(changeUser users)throws Exception{//同意客户端改为商户端
        userMapper userMapper = new userSql();
        //先查询一下状态是不是未处理
        ResultSet rs = userMapper.selectChangeUser(users.getUserID());
        if(rs.next()){
            int statue1 = rs.getInt("statue");
            if(statue1 == 1){
                return 0;//已处理
            }
            else {
                userMapper.updateChangeUserStatue(users.getUserID());//改变状态
                userMapper.updateUserPanduan(users.getUserID());//客户端变成商户端
                return 1;
            }
        }
        return 2;
    }
    public int cancelAccount(users users)throws Exception{//封号了
        userMapper userMapper = new userSql();
        //先查询一下是不是封号了
        ResultSet rs = userMapper.selectAllById(users.getUserID());
        if(rs.next()){
            if(rs.getInt("panduan") == 3){
                //已经是封号了
                return 0;
            }
            else {
                userMapper.updateUserPanduan2(users.getUserID());
                return 1;
            }
        }
        return 2;
    }
    public List<usersPrice> manageUserPrice()throws Exception{
        userMapper userMapper = new userSql();
        return userMapper.selectAllUsersPrice();
    }
    public int agreeAddMoney(usersPrice usersPrice)throws Exception{//通过充值
        userMapper userMapper = new userSql();
        ResultSet rs = userMapper.selectUserPrice(usersPrice.getPriceID());
        if(rs.next()){
            if(rs.getInt("price_statue") == 1){
                return 0;//这个充值已经被处理了
            }
            else {
                ResultSet rs2 = userMapper.selectAllById(usersPrice.getUserID());
                if(rs2.next()){
                    int num = rs2.getInt("money");//获取原来的钱数
                    userMapper.updateUserMoney((num+usersPrice.getPrice()),usersPrice.getUserID());//更新钱
                    userMapper.updateUserPriceStatue(usersPrice.getPriceID());//更新充值的状态
                }
                return 1;
            }
        }
        return 2;
    }
    public List<sign> showSignNum()throws Exception{
        userMapper userMapper = new userSql();
        return userMapper.selectAllSignNum();
    }
}
